package com.ruoyi.system.services;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 菜单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysUserServices
{
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private ISysRoleService sysRoleService;


    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 同步用户信息
     * @return
     */
    public boolean synUser(String userIds) {
        Long[] userIdAry = null;
        if (StringUtils.isNotEmpty(userIds)) {
            userIdAry = Arrays.stream(userIds.split(",")).map(Long::parseLong).toArray(Long[]::new);
        }
        List<SysUser> userList = selectAllUser(userIdAry);
        return sysUserService.saveAndUpdateUser(userList);
    }

    public List<SysUser> selectAllUser(Long[] userIds) {
        return this.userMapper.selectAllUser(userIds);
    }


    /**
     * 同步菜单信息
     * @return
     */
    public boolean synMenu() {
        List<SysMenu> menuList = selectAllMenus();
        return sysMenuService.saveAndUpdateMenu(menuList);
    }
    public List<SysMenu> selectAllMenus() {
        return menuMapper.selectMenuList(new SysMenu());
    }


    /**
     * 同步部门信息
     * @return
     */
    public boolean synDept() {
        List<SysDept> deptList = selectAllDept();
        return sysDeptService.saveAndUpdateDept(deptList);
    }
    public List<SysDept> selectAllDept() {
        return deptMapper.selectDeptList(new SysDept());
    }


    /**
     * 同步角色信息
     *
     * @return
     */
    public boolean synRole() {
        List<SysRole> roleList = selectAllRoles();
        return sysRoleService.saveAndUpdateRole(roleList);
    }

    public List<SysRole> selectAllRoles() {
        return roleMapper.selectAllRole();
    }

}
